const util = require('../../utils/util.js');
const api = require('../../config/api.js');
var check = require('../../utils/check.js');

var initSubject = '请选择科目';
var initClassDate = '请选择日期';
var initClassTime = '请选择时间';
var initCoach = '请选择教练';
var app = getApp();

Page({
  data: {
    subjects: ["科二", "科三"],
    selSubjectIndex: 0,
    selSubject: initSubject,

    classDate: [],
    selClassDateIndex: 0,
    selClassDate: initClassDate,
    selClassDateSign:false,

    classTime: [],
    selClassTimeIndex: 0,
    selClassTime: initClassTime,
    selClassTimeSign:false,

    coachIds: [],
    coachs: [],
    selCoachIndex: 0,
    selCoachId: 0,
    selCoachIdSign:false,
    selCoach: initCoach,
    navbar_bgcolor: wx.getStorageSync('navbar_bgcolor'),
    userName: '',
    phone: ''
  },

  onLoad: function (param) {
    var username = param.userName;
    var phone=param.phone
    var that = this;
    that.setData({
      userName: username,
      phone: phone
    });
  },

  bindCancel: function () {
    wx.navigateBack({})
  },

  bindSave: function (e) {
    var hasLogin = wx.getStorageSync("app.globalData.hasLogin");
    if (!hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }

    var that = this;
    var mobile = e.detail.value.mobile;

    if (mobile == "") {
      util.showErrorToast('请填写手机号码');
      return;
    }

    if (!check.isValidPhone(mobile)) {
      util.showErrorToast('手机号码不正确');
      return;
    }

    if (that.data.selSubject == initSubject) {
      util.showErrorToast('请选择科目');
      return;
    }

    if (that.data.selClassDate == initClassDate) {
      util.showErrorToast('请选择日期');
      return;
    }

    if (that.data.classTime == initClassTime) {
      util.showErrorToast('请选择时间');
      return;
    }

    if (that.data.selCoach == initCoach) {
      util.showErrorToast('请选择教练');
      return;
    }

    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    util.request(api.AppointmentSave, {
      phone: mobile,
      subject: that.data.selSubject,
      trainDate: that.data.selClassDate,
      trainTime: that.data.selClassTime,
      coachId: that.data.selCoachId,
      coachName: that.data.selCoach,
      uid:userid
    }, "POST")
      .then(function (res) {
        if (res.errno == 0) {
          wx.showModal({
            title: '提示',
            content: '预约成功！',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            }
          })
        } else {
          util.showErrorToast(res.errmsg);
        }
      }).catch(err=>{
          util.showErrorToast(err.errmsg);
      });

  },

  bindPickerSubjectChange: function (event) {
    var hasLogin = wx.getStorageSync("app.globalData.hasLogin");
    if (!hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
    var that = this;

    var selItem = this.data.subjects[event.detail.value];
    that.setData({
      selSubject: selItem,
      selSubjectIndex: event.detail.value == "0" ? "2" : "3"
    })
    that.dataFilter();
  },
  bindPickerClassDateChange: function (event) {
    var selItem = this.data.classDate[event.detail.value];
    this.setData({
      selClassDate: selItem,
      selClassDateIndex: event.detail.value
    })
    this.dataFilter();
  },
  bindPickerClassTimeChange: function (event) {
    var selItem = this.data.classTime[event.detail.value];
    this.setData({
      selClassTime: selItem,
      selClassTimeIndex: event.detail.value
    })
    this.dataFilter();
  },
  bindPickerCoachChange: function (event) {
    var selCoachItem = this.data.coachs[event.detail.value];
    var selCoachIdItem = this.data.coachIds[event.detail.value];

    this.setData({
      selCoach: selCoachItem,
      selCoachId: selCoachIdItem,
      selCoachIndex: event.detail.value
    })
  },
  toRecord: function () {
    wx.navigateTo({
      url: '/pages/train/record/index',
    })
  },
  dataFilter: function () {
    var that = this;
    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    util.request(api.CourseFilter, {
      subject: that.data.selSubjectIndex,
      trainDate: that.data.selClassDate,
      trainTime: that.data.selClassTime,
      coachId: that.data.selCoach,uid:userid,
      key:api.key
    })
      .then(function (res) {
        if (res.errno == 0) {
          var data = res.data;
          that.setData({
            classDate: data.classDateList,
            classTime: data.classTimeList,
            coachIds: data.coachIdList,
            coachs: data.coachNameList,
            selClassDateSign:data.classDateList.length>0?false:true,
            selClassTimeSign:data.classTimeList.length>0?false:true,
            selCoachIdSign:data.coachNameList.length>0?false:true
          });
          console.log(that.data.selClassDateSign);
        } else if(res.errno === 402){
          wx.showModal({
            title: '错误',
            content:'不被允许的请求',
            showCancel: false
          })
      }else {
          util.showErrorToast(res.errmsg);
        }
      });
  }
})